package com.yanyilu.basing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanyilu.basing.common.R;
import com.yanyilu.basing.common.ResultCode;
import com.yanyilu.basing.entity.Leaderboard;
import com.yanyilu.basing.entity.TotalTimeMileage;
import com.yanyilu.basing.mapper.LeaderboardMapper;
import com.yanyilu.basing.service.ILeaderboardService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sxx
 * @since 2020-10-26
 */
@Service
public class LeaderboardServiceImpl extends ServiceImpl<LeaderboardMapper, Leaderboard> implements ILeaderboardService {
    @Autowired
    LeaderboardMapper leaderboardMapper;

    @Override
    public R getList(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Leaderboard> list = leaderboardMapper.selectAllOrderByRank();
        PageInfo<Leaderboard> pageInfo = new PageInfo<>(list);
        return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
    }

    /**
     * 获取排行榜列表
     * @param userId
     * @param limit
     * @param page
     * @return
     */
    @Override
    public R getList(Integer userId, Integer limit, Integer page) {
        PageHelper.startPage(page,limit);
        List<Leaderboard> list = leaderboardMapper.selectAllOrderByRank();
        PageInfo<Leaderboard> pageInfo = new PageInfo<>(list);
        return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());

    }

    @Override
    public R getInfo(Integer userId) {
        return null;
    }

    //加入排行榜且修改所有人排名
    @Override
    public R adminAdd(TotalTimeMileage totalTimeMileage) {
        Leaderboard leaderboard = leaderboardMapper.selectByUserId(totalTimeMileage.getUserId());

        if(null!=leaderboard){
            leaderboardMapper.deleteById(leaderboard.getLeaderboardId());
        }

        Integer number = leaderboardMapper.countByTomil(totalTimeMileage.getTotalTime());
        Leaderboard lb = new Leaderboard();
        lb.setRank(number+1);
        lb.setUserId(totalTimeMileage.getUserId());
        lb.setUserName(totalTimeMileage.getUserName());
        lb.setTotalTime(totalTimeMileage.getTotalTime());
        lb.setHeadImage(totalTimeMileage.getHeadImage());
        lb.setTotalMileage(totalTimeMileage.getTotalMileage());
        lb.setWriteOffStatus(0);
        leaderboardMapper.insert(lb);

        leaderboardMapper.updateRankByMil(totalTimeMileage.getTotalMileage());

        return R.ok();
    }



    @Override
    public R adminList(Integer page,Integer limit,String contactDetails) {

        PageHelper.startPage(page,limit);
        List<Leaderboard> list = leaderboardMapper.selectBycontactDetails(contactDetails);
        PageInfo<Leaderboard> pageInfo = new PageInfo<>(list);
        return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
    }

    @Override
    public R adminUpdate(Leaderboard leaderboard) {
        Leaderboard leaderboard1 = leaderboardMapper.selectById(leaderboard.getLeaderboardId());
        if(null!=leaderboard1){
            leaderboard1.setWriteOffStatus(leaderboard.getWriteOffStatus());
            leaderboardMapper.updateById(leaderboard1);
            return R.ok();
        }else {
            return R.ok(ResultCode.RESULT_DATA_NONE);
        }

    }

    @Override
    public R adminDeleteByUserId(Integer userId) {
        leaderboardMapper.adminDeleteByUserId(userId);
        return R.ok();
    }

}
